package com.ssm.base.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.base.Base;
import com.ssm.base.model.Page;
import com.ssm.domain.Specifications;

import tk.mybatis.mapper.common.Mapper;

@Transactional
public class BaseService<T> extends Base implements IBaseService<T>{

	private Mapper<T> mapper;

	public BaseService(Mapper<T> mapper){
		this.mapper = mapper;
	}

	public T selectOne(T entity) {
		return mapper.selectOne(entity);
	}

	public PageInfo<T> selectAll(Page page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return new PageInfo<T>(mapper.selectAll(), Page.NAV_SIZE);
	}

	public List<T> selectAll() {
		return mapper.selectAll();
	}

	public T selectByPrimaryKey(Object key) {
		return mapper.selectByPrimaryKey(key);
	}

	public List<T> select(T entity) {
		return mapper.select(entity);
	}

	public PageInfo<T> select(T entity, Page page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return new PageInfo<T>(mapper.select(entity), Page.NAV_SIZE);
	}

	public PageInfo<T> selectByExample(Object example, Page page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return new PageInfo<T>(mapper.selectByExample(example), Page.NAV_SIZE);
	}

	public List<T> selectByExample(Object example) {
		return mapper.selectByExample(example);
	}
	


	public int selectCount(T entity) {
		return mapper.selectCount(entity);
	}

	public int delete(T entity) {
		return mapper.delete(entity);
	}

	public int deleteByPrimaryKey(Object key) {
		return mapper.deleteByPrimaryKey(key);

	}

	public int deleteByPrimaryKeys(List<?> ids) {
		int sum = 0;
		for(Object id : ids){
			sum += mapper.deleteByPrimaryKey(id);
		}
		return sum;
	}

	public int deleteByExample(Object example) {
		return mapper.deleteByExample(example);
	}

	public int updateByPrimaryKey(T entity) {
		return mapper.updateByPrimaryKey(entity);
	}

	public int updateByExample(T entity, Object example) {
		return mapper.updateByExample(entity, example);
	}

	public int updateByPrimaryKeySelective(T entity) {
		return mapper.updateByPrimaryKeySelective(entity);
	}

	public int updateByExampleSelective(T entity, Object example) {
		return mapper.updateByExampleSelective(entity, example);
	}

	public int insert(T entity) throws Exception {
		return mapper.insert(entity);
	}
	
	public int insertBatch(List<T> list){
		int sum = 0;
		for(T entity : list){
			sum += mapper.insertSelective(entity);
		}
		return sum;
	}

	public int insertSelective(T entity) {
		return mapper.insertSelective(entity);
	}

	public int selectCountByExample(Object example) {
		return mapper.selectCountByExample(example);
	}

	



}
